import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import javax.swing.tree.TreeNode;

public class AgentNode implements TreeNode
{
	private List<MonitorDataNode> m_childs;
	private String m_host;
	private String m_ip;
	private boolean m_isAlive;
	private TreeNode m_parent;
	
	public AgentNode(TreeNode parent, String host,String ip)
	{
		m_host = host;
		m_ip = ip;
		m_isAlive = true;
		m_parent = parent;
		m_childs = new ArrayList<MonitorDataNode>();
	}
	
	public boolean getIsAlive()
	{
		return m_isAlive; 
	}
	
	public void setIsAlive(boolean isAlive)
	{
		m_isAlive = isAlive;
		for(MonitorDataNode node:m_childs)
		{
			node.setIsAlive(isAlive);
		}
	}
	
	public String getHost()
	{
		return m_host;
	}
	
	public String getIP()
	{
		return m_ip;
	}
	
	public void setHost(String host)
	{
		m_host = host;
	}
	
	public void setIP(String ip)
	{
		m_ip = ip;
	}
	
	public void add(MonitorDataNode monitorData)
	{
		monitorData.setIsAlive(getIsAlive());
		m_childs.add(monitorData);
	}
	
	public List<MonitorDataNode> getChilds()
	{
		return m_childs;
	}
	
	public String getText()
	{
		return String.format("%s[%s]", m_host,m_ip);
	}
	
	public String toString()
	{
		return getText();
	}
	
	@Override
	public boolean equals(Object obj)
	{
		if(obj == null)
			return false;
		
		if (!(obj instanceof AgentNode))
			return false;
		
		AgentNode other = (AgentNode)obj;
		return getText().equals(other.getText());
	}
	
	public int hashCode()
	{
		return getText().hashCode();
	}

	@Override
	public TreeNode getChildAt(int childIndex) 
	{
		return m_childs.get(childIndex);
	}

	@Override
	public int getChildCount() 
	{
		return m_childs.size();
	}

	@Override
	public TreeNode getParent() 
	{	
		return m_parent;
	}

	@Override
	public int getIndex(TreeNode node) 
	{
		return m_childs.indexOf(node);
	}

	@Override
	public boolean getAllowsChildren() 
	{
		return true;
	}

	@Override
	public boolean isLeaf() 
	{
		return false;
	}

	@Override
	public Enumeration children() 
	{
		return Collections.enumeration(m_childs);
	}
}

